package com.tudou.searchcontrol.dao;



/**
 * zwu
 */


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.tudou.searchcontrol.model.PageInfo;

public class BaseDao<T> extends SqlSessionDaoSupport{
	
	public String getNameSpace() {
		return "";
	}
	
	public T getObjectById(Long id){
		return (T)this.getSqlSession().selectOne(this.getNameSpace()+".getById", id);
		
	}
	
	public T getObject(Map searchMap){
		return (T)this.getSqlSession().selectOne(this.getNameSpace()+".getObject", searchMap);
		
	}
	
	public PageInfo getObjectPageInfo(Map searchMap,PageInfo pageInfo){
		List list = getObjectList(searchMap);
		Integer totalPage = 0;
		int pageSize = pageInfo.getPageSize();
		int pagecount = 0;
		if(list != null){
			totalPage = list.size()/pageSize;
			if(list.size()%pageSize != 0){
				totalPage += 1;
			}
			pagecount = list.size();
		}
		int currpage = pageInfo.getCurrpage();
		if(searchMap == null) searchMap = new HashMap();
		searchMap.put("startNum", pageSize*(currpage-1));
		searchMap.put("pageSize", pageSize);
		
		pageInfo.setDataList((List<T>)this.getSqlSession().selectList(this.getNameSpace()+".getPage", searchMap));
		pageInfo.setTotalPage(totalPage);
		pageInfo.setPagecount(pagecount);
		return pageInfo;
	}

	public List<T> getObjectList(Map<String,Object> searchMap){
		return (List<T>)this.getSqlSession().selectList(this.getNameSpace()+".getList", searchMap);
	}
	
	public int addObject(T t){
		return this.getSqlSession().insert(this.getNameSpace()+".insert", t);
		
	}
	
	public int updateObject(T t){
		return this.getSqlSession().update(this.getNameSpace()+".update", t);
		
	}
	
	public int deleteObject(Long id){
		return this.getSqlSession().delete(this.getNameSpace()+".delete", id);
		
	}
	
}
